/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.sensor.internal.temperature;

import java.util.logging.Logger;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 26 mai 2010
 * Time: 18:12:40
 * To change this template use File | Settings | File Templates.
 */
@Component
@Provides
public class TemperatureConverter {
    private static final Logger logger = Logger.getLogger(TemperatureConverter.class.getName());

    public static final String SI_CELSIUS = "SI-C";
    public static final String SI_KELVIN = "SI-K";

    public static double convert(String sensorModel, int value) {
        if (sensorModel.equals(SI_KELVIN)) {
            // Store as is.
            return value;
        }
        else if (sensorModel.equals(SI_CELSIUS)) {
            // Convert to kelvin.
            return value + 273.15;
        }
        else {
            // SHT1x family, 14-bit raw temperature measurement, assumed 3.5V VDD.
            if (sensorModel.equals("U14-SHT1x")) {
                return (-39.7 + 0.01 * (double)value) + 273.15;
            }
            // SHT1x family, 12-bit raw temperature measurement, assumed 3.5V VDD.
            if (sensorModel.equals("U12-SHT1x")) {
                return (-39.7 + 0.04 * (double)value) + 273.15;
            }

            // Complain about unknown sensor models.
            logger.severe("Unknown sensor model \"" + sensorModel + "\", storing raw value as is !");
            return value;
        }
    }
}
